package com.ac.api.user.service.impl;

import com.ac.api.userinfo.service.AcUserinfoService;
import com.ac.utils.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.context.Context;
import com.base.context.ContextHolder;
import com.base.helper.Result;
import com.base.helper.StringUtils;
import com.ac.api.group.entity.AcGroup;
import com.ac.api.group.service.AcActionPageService;
import com.ac.api.group.service.AcGroupService;
import com.ac.api.user.service.AcLoginlogService;
import com.ac.api.user.service.MbMemberService;
import com.ac.api.user.dao.AcUserMapper;
import com.ac.api.user.entity.AcUser;
import com.ac.api.user.service.AcUserService;
import com.ac.api.user.vo.*;
import com.ac.utils.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
@Slf4j
public class AcUserServiceImpl extends ServiceImpl<AcUserMapper, AcUser> implements AcUserService {
    @Autowired
    private AcUserMapper acUserMapper;
    @Autowired
    private AcUserinfoService acUserinfoService;
    @Autowired
    private MbMemberService mbMemberService;
    @Autowired
    private AcGroupService acGroupService;
    @Autowired
    private AcActionPageService acActionPageService;
    @Autowired
    private AcLoginlogService acLoginlogService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    WxService wxService;
    private static final String appid = "wxc1c99e27fac3b752";
    private static final String secret = "b65ebe9f2ad928301d162f0c6093faea";

    @Override
    public Integer getUserId() {
        Context context = ContextHolder.currentContext();
        Integer userId=context.currentUser().getId();
       //如果无法获取，则默认一个用户，测试专用，正式环境必须屏蔽
        if (context.currentToken().equals("fbc545a91cc94fe89296828a25a7e08e@9085553879028596738")){
            userId=96;
        }
        return userId;
    }

    private LoginSuccessVo normalizeContext(Context context, AcUser user) {
        AcUserVo vo      = new AcUserVo(user);

        Map<String, Object> claims = new HashMap<>();
        claims.put("id", vo.getId());
        claims.put("username", vo.getUsername());
        claims.put("grouptype", vo.getGrouptype());
        claims.put("usergroup", vo.getUsergroup());
        String token = JWTUtil.genToken(claims);
        user.setPassword(null);
        return new LoginSuccessVo(user,"",token);
    }

    private QueryWrapper<AcUserVo> buildWrapper(AcUserVo example) {
        QueryWrapper<AcUserVo> wrapper = new QueryWrapper<>();
        String third_id=example.getThird_id();
        String username = example.getUsername();
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(third_id), AcUserVo::getThird_id, third_id)
                .like(StringUtils.isNotEmpty(username), AcUserVo::getUsername, username)
                .orderByDesc(AcUserVo::getId);

        // 搜索订单姓名
        String realname = example.getRealname();
        if (StringUtils.isNotEmpty(realname)) {
            wrapper.and(w -> w.like("realname", realname));
        }

        return wrapper;
    }

    @Override
    public AcUser findByUsername(String username) {
        return this.baseMapper.findByUsername(username);
    }
    @Override
    public AcUser setEnable(Integer id, String enable) {
        AcUser acUser = this.getById(id);
        if (null == acUser || "0".equals(acUser.getState())) {
            throw new RuntimeException("没有找到id对应对象");
        }
        this.updateById(acUser);
        return acUser;
    }

    @Override
    public AcUser registerExamineeUser(RegisterExamineeVo examinee) {
        AcUser acUser = new AcUser();
        acUser.setUsername(examinee.getUsername());
        acUser.setRealname(examinee.getRealname());
        acUser.setPassword(examinee.getIdentitynum().substring(examinee.getIdentitynum().length() - 6));
        acUser.setCreatetime(new Date());
        acUser.setState(1);
        acUser.setRoleid(examinee.getRoleid());
        acUser.setDepartment(examinee.getDepartment());
        if (this.save(acUser)) {
            return this.findByUsername(examinee.getUsername());
        } else {
            return null;
        }
    }

    @Override
    public AcUser registerStudentUser(RegisterStudentVo student) {
        AcUser acUser = new AcUser();
        acUser.setUsername(student.getUsername());
        acUser.setRealname(student.getName());
        acUser.setPassword(student.getExamineenum().substring(student.getExamineenum().length() - 6));
        acUser.setCreatetime(new Date());
        acUser.setState(1);
        if (this.save(acUser)) {
            return this.findByUsername(student.getUsername());
        } else {
            return null;
        }
    }

    @Override
    public AcUser updateUserRealname(Integer id, String realname) {
        AcUser acUser = this.getById(id);
        if (null != acUser && "1".equals(acUser.getState())) {
            acUser.setRealname(realname);
            this.updateById(acUser);
            return acUser;
        }
        return null;
    }

    @Override
    public List<AcUser> deleteByUserIds(List<Integer> userIds) {
        QueryWrapper<AcUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(AcUser::getId, userIds);
        List<AcUser> list = super.list(wrapper);
        this.removeByIds(userIds);
        return list;
    }

}
